﻿using System;
using System.Configuration;
using LaxCraft.Attributes;
using LaxCraft.Domain;
using LaxCraft.Helpers;

namespace LaxCraft.Controllers
{
	using System.Web.Mvc;
    using System.Linq;
	using LaxCraft.Repositories;

	public class TeamController : Controller
	{
		private readonly ITeamRepository team;

		public TeamController(ITeamRepository team)
		{
			this.team = team;
		}

		public ActionResult Index()
		{
		    ViewBag.Players = team.GetPlayers().OrderBy(p => p.Name).ToList();
            ViewBag.Stats = team.GetStats().OrderBy(s => s.Order).ToList();

            return View(ViewBag);
		}

        public ActionResult Player(int id)
        {
            ViewBag.Player = team.GetPlayers().Where(p => p.Id == id).FirstOrDefault();
            ViewBag.Stats = team.GetStats().OrderBy(s => s.Order).ToList();
            ViewBag.Edit = SecurityHelper.PlayerAccess(ViewBag.Player);
            ViewBag.Admin = LaxCraftSession.CurrentUser.IsAdministrator;

            return View("Player", ViewBag);
        }

        [HttpGet]
        public ActionResult StatEntry(int playerId, int statId)
        {
            var player = team.GetPlayers().Where(p => p.Id == playerId).FirstOrDefault();
            if (!SecurityHelper.PlayerAccess(player))
                return Player(playerId);

            ViewBag.Player = player;
            var playerStat = player.PlayerStats.Where(s => s.Stat.Id == statId).FirstOrDefault();
            if (playerStat == null) {
                var stat = team.GetStats().Where(s => s.Id == statId).FirstOrDefault();
                playerStat = new PlayerStat() { Stat = stat, XP = 0 };
            }
            ViewBag.PlayerStat = playerStat;

            return View(ViewBag);
        }

	    [HttpPost]
        public ActionResult StatEntry(FormCollection collection)
        {
            var player = team.GetPlayer(int.Parse(collection["PlayerId"]));
            var stat = team.GetStats().Where(s => s.Id == int.Parse(collection["StatId"])).FirstOrDefault();
            var xp = int.Parse(collection["XP"])*stat.UnitXP;

            var playerStat = player.PlayerStats.FirstOrDefault(s => s.Stat.Id == stat.Id);

            if (playerStat == null)
            {
                player.PlayerStats.Add(new PlayerStat() { Stat = stat, XP = xp + int.Parse(ConfigurationManager.AppSettings["XPFirstLevel"]) });
            }
            else
            {
                playerStat.XP += xp;
            }
            team.SavePlayer(player);

            var entry = new XPEntry()
                            {
                                Comment = collection["Comment"],
                                Date = DateTime.Now,
                                PlayerId = int.Parse(collection["PlayerId"]),
                                Stat = stat,
                                XP = xp
                            };
            team.SaveXPEntry(entry);
            return Player(int.Parse(collection["PlayerId"]));
        }
	}
}